package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.collection.ArraySet;
import easier.data.store.RxPreferencesStoreManager;
import easier.log.logger.Logger;
import io.reactivex.rxjava3.functions.Consumer;
import java.util.Set;

public class MainActivity45 extends AppCompatActivity {

    private Button mSave0;
    private Button mRead0;
    private EditText mInput0;
    private Button mDispose;
    private RxPreferencesStoreManager mManager;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity45.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main45 );

        initView();
        initViewAction();

        test();
    }

    private void test() {

        mManager.observeLong( "long" ).subscribe( new Consumer< Long >() {
            @Override
            public void accept( Long aLong ) throws Throwable {

                Logger.d( "key long --> " + aLong );
            }
        }, new Consumer< Throwable >() {
            @Override
            public void accept( Throwable throwable ) throws Throwable {

            }
        } );
        mManager.updateData( "long", 156L );
        /* ========================================================================= */

        mManager.observeDouble( "double" ).subscribe( new Consumer< Double >() {
            @Override
            public void accept( Double aLong ) throws Throwable {

                Logger.d( "key double --> " + aLong );
            }
        }, new Consumer< Throwable >() {
            @Override
            public void accept( Throwable throwable ) throws Throwable {

            }
        } );
        mManager.updateData( "double", 1990.826 );
        /* ========================================================================= */

        mManager.observeFloat( "float" ).subscribe( new Consumer< Float >() {
            @Override
            public void accept( Float aLong ) throws Throwable {

                Logger.d( "key Float --> " + aLong );
            }
        }, new Consumer< Throwable >() {
            @Override
            public void accept( Throwable throwable ) throws Throwable {

            }
        } );
        mManager.updateData( "float", 1990.826f );
        /* ========================================================================= */

        mManager.observeBoolean( "boolean", false ).subscribe( new Consumer< Boolean >() {
            @Override
            public void accept( Boolean aLong ) throws Throwable {

                Logger.d( "key Boolean --> " + aLong );
            }
        }, new Consumer< Throwable >() {
            @Override
            public void accept( Throwable throwable ) throws Throwable {

            }
        } );
        mManager.updateData( "boolean", false );
        /* ========================================================================= */

        mManager.observeString( "string" ).subscribe( new Consumer< String >() {
            @Override
            public void accept( String aLong ) throws Throwable {

                Logger.d( "key String --> " + aLong );
            }
        }, new Consumer< Throwable >() {
            @Override
            public void accept( Throwable throwable ) throws Throwable {

            }
        } );
        mManager.updateData( "string", "Hello this data" );
        /* ========================================================================= */

        mManager.observeStringSet( "Set<String>" ).subscribe( new Consumer< Set< String > >() {
            @Override
            public void accept( Set< String > aLong ) throws Throwable {

                Logger.d( "key Set<String> --> " + aLong );
            }
        }, new Consumer< Throwable >() {
            @Override
            public void accept( Throwable throwable ) throws Throwable {

            }
        } );

        ArraySet< String > data = new ArraySet<>();
        data.add( "first" );
        data.add( "second" );
        data.add( "third" );
        mManager.updateData( "Set<String>", data );
        /* ========================================================================= */
    }

    private void initView() {

        mSave0 = (Button) findViewById( R.id.save0 );
        mRead0 = (Button) findViewById( R.id.read0 );
        mInput0 = (EditText) findViewById( R.id.input0 );
        mDispose = (Button) findViewById( R.id.dispose );
    }

    private void initViewAction() {

        final String key = "example_counter";

        mManager = new RxPreferencesStoreManager( this, "settings" );
        mSave0.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                int i = Integer.parseInt( mInput0.getText().toString() );
                mManager.updateData( "int", i );
                mInput0.setText( "" );
            }
        } );
        mRead0.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                mManager.observeInt( "int" )
                    .subscribe( new Consumer< Integer >() {
                        @Override
                        public void accept( Integer integer ) throws Throwable {

                            Logger.d( integer );
                            mInput0.post( new Runnable() {
                                @Override
                                public void run() {

                                    mInput0.setText( integer.toString() );
                                }
                            } );
                        }
                    }, new Consumer< Throwable >() {
                        @Override
                        public void accept( Throwable throwable ) throws Throwable {

                            Logger.d( throwable );
                        }
                    } );
            }
        } );

        mDispose.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                mManager.dispose();
            }
        } );
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        mManager.dispose();
    }
}